package com.zzp.thread.syn;

public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        Thread thread = new Thread(buyTicket, "001");
        Thread thread2 = new Thread(buyTicket, "002");
        Thread thread3 = new Thread(buyTicket, "003");
        thread.start();
        thread2.start();
        thread3.start();
    }
}

class BuyTicket implements Runnable {

    private int ticketNums = 10;

    //线程外部停止方式
    boolean flag = true;

    @Override
    public void run() {
        //买票
        while (flag){
            buy();
        }
    }

    private void buy(){

        //判断是否有票
        if (ticketNums <= 0){

            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + "正在卖第" + ticketNums-- + "张票，剩余：" + ticketNums);
    }

}